/**
 * 用户注册Action
 * 当为单用户时:
 * 1.需要持久化用户信息, UserService
 * 2.需要初始化容量, CapacityService 单用户10G
 * 3.需要记录日志, LogService
 * 企业用户:
 * 转到EnterpriseUserRegisterAction
 * 处理
 */
package com.netdisk.controller;
import static com.netdisk.util.UUIDCreator.getUuId;
import static com.netdisk.util.RemoteAddress.getRemoteAddress;
import static com.netdisk.util.SystemParametersReader.getSingleUserMaxCapacity;
import static com.netdisk.util.SystemParametersReader.getSaveDir;
import java.util.Calendar;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.netdisk.entity.Capacity;
import com.netdisk.entity.FileSystem;
import com.netdisk.entity.Log;
import com.netdisk.entity.User;
import com.netdisk.service.impl.CapacityService;
import com.netdisk.service.impl.FileSystemService;
import com.netdisk.service.impl.UserService;
import com.netdisk.util.EncrypSHA;
import com.netdisk.vo.UserInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("userRegisterAction")
@Scope("prototype")
public class UserRegisterAction extends ActionSupport implements ModelDriven{
	
	private UserInfo userInfo = new UserInfo();
	
	private UserService userService;
	
	public UserService getUserService() {
		return userService;
	}
	
	@Resource(name="userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@Override
	public UserInfo getModel() {
		return userInfo;
	}
	
	@Override
	public String execute() throws Exception {
		boolean isExist = userService.existWithLoginName(userInfo.getLoginName());
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession	session =  request.getSession();
		System.out.println(userInfo);
		if(isExist){
			addFieldError("loginName","loginName");
			return INPUT;
		}else if(session.getAttribute("company") == null){
			System.out.println("singleUserRegister");
			String id = getUuId(); //生成uuid
			//从fromBean里面得到新用户
			User user = userInfo.getUser();
			user.setId(id);
			
			String ip = getRemoteAddress(request);
			//密码sha加密
			user.setLoginPassword( EncrypSHA.eccrypt ( userInfo.getLoginPassword() ) );
			
			Log userLog = new Log(getUuId(), (byte)2, id, 
					user.getLoginName(), Calendar.getInstance().getTimeInMillis(), ip);
			
			//初始化10G的容量
			long maxCapacity = getSingleUserMaxCapacity();
			
			Capacity cap = new Capacity(getUuId(), maxCapacity,
					0, user.getId(), user.getLoginName());
			
			//创建新建空间日志
			Log capLog = new Log(getUuId(), (byte)42, id, 
					user.getLoginName(), Calendar.getInstance().getTimeInMillis(), ip);
			
			String storeName = request.getRealPath(getSaveDir());
			storeName = storeName + "/" + getUuId();
			FileSystem fileSystem = new FileSystem(getUuId(), "..", storeName, (byte)1, "/", (byte)0, Calendar.getInstance().getTimeInMillis(), Calendar.getInstance().getTimeInMillis(), 
					user.getId(), user.getLoginName(), (byte)1, (byte)0, ""); 
			Log fileLog = new Log(getUuId(), (byte)32, user.getId(),
					user.getLoginName(), Calendar.getInstance().getTimeInMillis(), ip);
			userService.save(user, userLog, cap, capLog, fileSystem, fileLog);
			//标识改用户已经登录了
			session.setAttribute("logined", "single");
			session.setAttribute("user", user);
			return SUCCESS;
		}else{
			System.out.println("enterpriseUserRegister");
			return "enterprise";
		}
	}
	
	
}
